/** 
 * This file is a part of qloudgen-sec. 
 * You can redistribute qloudgen-sec and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-sec is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.sec.util;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * <p>Title: JNDI tools.</p>
 * <p>Description: Implementation common jndi method.</p>
 * <p>Copyright: qloudgen/sec 2011</p>
 * @author <a href="mailto:hyq.dd.sun@gmail.com">glue.3M</a>
 */
public final class JndiUtil {

	/**
	 * Returns context by default config file jndi.properties.
	 *
	 * @return Context
	 */
	public static Context getContext() throws Exception {
		return getContext( null );
	}

	/**
	 * Returns context by custom config file.
	 *
	 * @param configFileName
	 * @return Context
	 */
	public static Context getContext(String configFileName) throws Exception {
		Properties p = null;
		//
		if ( configFileName != null ) {
			p = new Properties();
			p.load( JndiUtil.class.getResourceAsStream( configFileName ) );
		}
		//
		if ( p == null ) {
			return new InitialContext();
		}
		else {
			return new InitialContext( p );
		}
	}
}